package com.yindian.cos.easy.security.authentication.login.social.core;

import com.yindian.cos.easy.security.authentication.login.social.qq.QqLoginAuthentication;
import com.yindian.cos.easy.security.authentication.login.social.weibo.WeiBoLoginAuthentication;
import com.yindian.cos.easy.security.authentication.login.social.weixin.WeiXinLoginAuthentication;
import com.yindian.cos.easy.security.constant.ProviderName;
import com.yindian.cos.easy.security.context.AuthenticationContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 获取第三方用户信息后的处理器，提供了默认实现，用户也可以自己继承该类注册为spring组件
 *
 * @author zhangchuanhao
 * @create 2018/11/20
 */
public abstract class AbstractPostProcessor {

    public void processor(HttpServletRequest request, HttpServletResponse response, UserInfo userInfo) throws IOException {
        String provider = userInfo.getProvider();
        AuthenticationContext ctx = AuthenticationContext.getCurrentContext();
        if (provider.equals(ProviderName.QQ)) {
            ctx.setAuthentication(QqLoginAuthentication.class.getName());
        } else if (provider.equals(ProviderName.WEIXIN)) {
            ctx.setAuthentication(WeiXinLoginAuthentication.class.getName());
        } else if (provider.equals(ProviderName.WEIBO)) {
            ctx.setAuthentication(WeiBoLoginAuthentication.class.getName());
        }
        processorHandler(request, response, userInfo);
    }

    /**
     * 具体的处理方法抽象
     *
     * @param request
     * @param response
     * @param userInfo
     */
    public abstract void processorHandler(HttpServletRequest request, HttpServletResponse response, UserInfo userInfo) throws IOException;
}
